package com.entrogy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.BannerEntity;
import com.entrogy.bean.SchoolEntity;
import com.entrogy.bean.SchoolThdidUrlEntity;
import com.entrogy.dao.SchoolMapper;
import com.entrogy.dao.StudentMapper;
import com.entrogy.service.SchoolService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SchoolServiceImpl implements SchoolService {
	@Autowired
	private SchoolMapper schoolMapper;

	@Autowired
	private StudentMapper studentMapper;

	@Override
	public int insertSchool(SchoolEntity schoolEntity) {
		return schoolMapper.insertSchool(schoolEntity);
	}

	@Override
	public Integer getSchoolCode(int schoolCode) {
		return schoolMapper.getSchoolCode(schoolCode);
	}

	@Override
	public List<SchoolEntity> getSchoolByUserId(String userId) {
		return schoolMapper.getSchoolByUserId(userId);
	}

	@Override
	public int updateSchool(SchoolEntity schoolEntity) {
		return schoolMapper.updateSchool(schoolEntity);
	}

	@Override
	public int deleteSchool(int schoolCode) {
		return schoolMapper.deleteSchool(schoolCode);
	}
	/**
	 *  获取学校列表
	 * @param map
	 * @return
	 */
	@Override
	public List<SchoolEntity> getSchoolList(Map<String,Object> map){
		return schoolMapper.getSchoolList(map);
	};

	/**
	 *   获取学校总数
	 * @param map
	 * @return
	 */
	@Override
	public int getSchoolListByTotal(Map<String,Object> map){
		if (schoolMapper.getSchoolListByTotal(map)==null){
			return 0;
		}
		return schoolMapper.getSchoolListByTotal(map);
	}

	@Override
	public int insertBanner(int type, Integer id,String imgUrl) {
		int schoolCode = (int) SecurityUtils.getSubject().getSession().getAttribute("schoolCode");
		if(type == 0){
			return schoolMapper.insertBanner(imgUrl,schoolCode);
		}
		return schoolMapper.updateBanner(imgUrl,id,schoolCode);
	}

	@Override
	public List<BannerEntity> getBanner(int schoolCode) {
		return schoolMapper.getBanner(schoolCode);
	}

	@Override
	public int deleteBanner(int id) {
		return schoolMapper.deleteBanner(id);
	}

	@Override
	public List<JSONObject> listSchool(Integer schoolCode) {
		return schoolMapper.listSchool(schoolCode);
	}

	@Override
	public SchoolThdidUrlEntity getSchoolThdidUrlAndGroupInfo(Integer schoolCode, String deviceModel) {
		return schoolMapper.getSchoolThdidUrlAndGroupInfo(schoolCode, deviceModel);
	}

    @Override
    public String getShcoolThdidUrl(Integer schoolCode, String deviceModel) {
        return schoolMapper.getSchoolThdidUrl(schoolCode, deviceModel);
    }

	@Override
	public String getSchoolName(Integer schoolCode) {
		return schoolMapper.getSchoolName(schoolCode);
	}

	@Override
	public Integer countStudent(Integer schoolCode, Integer gradeId, Integer courseId, String studentName) {
		return studentMapper.countStudent(schoolCode, gradeId, courseId, studentName);
	}

	@Override
	public Integer getSchoolOpenStatus(Integer schoolCode) {
		return schoolMapper.getSchoolOpenStatus(schoolCode);
	}

}
